import Vue from 'vue'
import VueRouter from "vue-router"
Vue.use(VueRouter)

import Home from "../views/Home.vue"
import Shopcar from "../views/Shopcar.vue"
import Users from "../views/Users.vue"
import Address from "../views/Address.vue"
import addAddress from "../views/addAddress.vue"
import editAddress from "../views/editAddress.vue"
import GoodsDetail from "../views/GoodsDetail.vue"
import Goodslist from "../views/Goodslist.vue"
// import Scroll from "../views/Scroll.vue"
import vantscroll from "../views/vantscroll.vue"
import Search from "../views/Search.vue"
import SearchResult from "../views/SearchResult.vue"
import store from "../store/index.js"
import orderList from "../views/orderList.vue"
import orderDetail from "../views/orderDetail.vue"
import Index from "../views/Index.vue"

// 创建路由器对象
const router = new VueRouter({
    routes: [{
            path: "/",
            redirect:"/home/index",
            component: Index,
            children:[
                {
                    path: "/home/index",
                    component: Home,
                    name: "Home",
                    meta: {
                        pageTitle: "首页",
                        mainPage: true,
                        isShowNavBar: false,

                    },
                    // 子路由匹配的内容要展示在父组件中的router-view中
                    children: [
                    {
                        path: "search",
                        component: Search,
                        name: "Search",
                        meta: {
                            pageTitle: "搜索",
                            isShowNavBar: false,
                        },
                    }]
                }, 
                {
                    path: "/home/shopcar",
                    component: Shopcar,
                    name: "Shopcar",
                    meta: {
                        pageTitle: "购物车",
                        mainPage: true,
                        isShowNavBar: true

                    }
                }, 
                {
                    path: "/home/users",
                    component: Users,
                    name: "Users",
                    meta: {
                        pageTitle: "个人中心",
                        mainPage: true,
                        isShowNavBar: false,
                        requireAuth: true
                    }
                },
            ]
        },
        {
            path: "/search-result/:keyword",
            component: SearchResult,
            name: "SearchResult",
            meta: {
                pageTitle: "搜索结果",
                isShowNavBar: false,
            },
        },
        {
            path: "/address",
            component: Address,
            meta: {
                pageTitle: "我的地址",
                mainPage: false,
                isShowNavBar: true,
                requireAuth: true
            },
        },
        {
            path: "/goodsDetail/:id",
            component: GoodsDetail,
            name:"goodsDetail",
            props: true,
            meta: {
                pageTitle: "商品详情",
                isShowNavBar: true
            }
        },
        {
            path: "/goodslist",
            component: Goodslist,
            meta: {
                pageTitle: "乐淘超市",
                isShowNavBar: true,
                requireAuth: false
            }
        },

        {
            path: "/vantscroll",
            component: vantscroll,
            meta: {
                pageTitle: "vant-scroll",
                isShowNavBar: true
            }
        },
        {
            path: "/newslist",
            component: () => import('../views/Newslist.vue'),
            meta: {
                pageTitle: "新闻列表",
                isShowNavBar: true
            }
        },
        {
            path: "/newsdetail/:id",
            component: () => import('../views/NewsDetail.vue'),
            props: true,
            meta: {
                pageTitle: "新闻详情",
                isShowNavBar: true
            }
        },
        {
            path: "/login",
            component: () => import('../views/Login.vue'),
            props: true,
            meta: {
                pageTitle: "用户登录",
                isShowNavBar: true
            }
        },
        {
            path: "/register",
            component: () => import('../views/Register.vue'),
            props: true,
            meta: {
                pageTitle: "用户注册",
                isShowNavBar: true
            }
        },
        {
            path: "/addAddress",
            component: addAddress,
            meta: {
                pageTitle: "添加地址",
                isShowNavBar: true
            }
        },
        {
            path: "/editAddress/:addressInfo",
            props: true,
            component: editAddress,
            meta: {
                pageTitle: "编辑地址",
                isShowNavBar: true
            }
        },
        {
            path: "/orderList/",
            component: orderList,
            meta: {
                pageTitle: "我的订单",
                isShowNavBar: true,
                requireAuth: true
            }
        },
        {
            path: "/orderDetail/:order_id",
            component: orderDetail,
            props:true,
            meta: {
                pageTitle: "订单详情",
                isShowNavBar: true,
                requireAuth: true
            }
        }
    ]
})


NProgress.configure({
    showSpinner: false
});

// 前守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    // console.log('beforeEach to.meta.requireAuth:',to.meta.requireAuth)
    let isRequireAuth = !!to.meta.requireAuth
    if (isRequireAuth) {
        if (store.state.token){
            next();
        }else{
            // next({
            //     path:"/login?redirect?" + to.fullPath
            // })
            router.push("/login?redirect?" + to.fullPath)
        }
    }else {
        next();
    }
    
})


// 后守卫
router.afterEach((to, from) => {
    NProgress.done();
})

// 暴露出去

export default router;
